<?php
    include ('../inc/config.php'); 

	if (!isset($_SESSION["admin"])) {redirect("login.php");	exit(); }
    
	$nombre_temporal = time().'_mensajeria_correos_unicos.xls';
	$destino = ROOT.'/temp/'.$nombre_temporal;

	$sql = "select * from base_mailing where email!='' group by email order by email ASC";
	$rs  = $db->Execute($sql);
	$data = $rs->GetRows();
	
	$fp = fopen("$destino", 'wb');
	$renglon = "<table><tr style='background:#683D85;color:#FFF;'>";
	$renglon.= "<td style='height:24px;'>Apellido</td>";  
	$renglon.= "<td>Nombre</td>"; 
	$renglon.= "<td>Email</td>";   
	$renglon.= "<td>Telefono</td>";
	$renglon.= "<td>Fecha</td></tr>";
	fwrite($fp, $renglon);

	foreach($data as $b) {
		$color = iif($color=='FEFFCC','FFF','FEFFCC');
		$renglon = "<tr style='background:#$color;color:#000;'>";
		$renglon.= "<td>{$b['apellido']}</td>";  
		$renglon.= "<td>{$b['nombre']}</td>"; 
		$renglon.= "<td>{$b['email']}</td>";   
		$renglon.= "<td>{$b['telefono']}</td>";
		$renglon.= "<td>{$b['fecha']}</td></tr>";
		fwrite($fp, utf8_decode($renglon) );
	}

	fclose($fp);
	$file = URL.'/temp/'.$nombre_temporal;
	$fecha = date("d-m-Y");
	$nombre = 'Mensajeria_correos_unicos_'.$fecha.'.xls';
	header("Content-Type: application/force-download");
	header("Content-disposition: attachment; filename=$nombre");
	header("Content-type: application/octet-stream");
	readfile($file);
		
	unlink($destino);

	die();
?>